package com.ant.storage.controller;

import com.alibaba.excel.EasyExcel;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.InventoryDto;
import com.ant.storage.dto.InventoryExcelDto;
import com.ant.storage.excel.InventoryCensusExecl;
import com.ant.storage.pojo.Inventory;
import com.ant.storage.service.IInventoryLogService;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.service.impl.GoodsListener;
import com.ant.storage.vo.InventoryCensusVo;
import com.ant.storage.vo.WarehouseAndCommodityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/inventory")
@Api(tags = "物品库存接口")
public class InventoryController {

    @Autowired
    private IInventoryService inventoryService;

    @Autowired
    private IInventoryLogService inventoryLogService;

    @ApiOperation(value = "获取物品库存数据列表")
    @GetMapping("/getInventoryList")
    public ResultPage getInventoryList(InventoryDto inventoryDto) {
        return ResultPage.success("获取成功", inventoryService.querylist(inventoryDto));
    }

    @ApiOperation(value = "通过仓库id获取仓库的库存")
    @GetMapping("/getInventoryBywarehouseId/{warehouseId}")
    public ResultVo getInventoryList(@PathVariable("warehouseId") Integer warehouseId) {
        List<Inventory> list = inventoryService.list(new QueryWrapper<Inventory>().eq("warehouse_id",
                warehouseId));
        return ResultVo.success("获取成功", list);
    }

    @ApiOperation(value = "查看出入明细")
    @GetMapping("/findAccessDetailed")
    public ResultVo findAccessDetailed(InventoryDto inventoryDto) {
        return ResultVo.success("获取成功", inventoryLogService.queryDetailedList(inventoryDto));
    }

    @ApiOperation(value = "统计")
    @GetMapping("/censusList")
    public ResultVo censusList(InventoryDto dto) {
        List<InventoryCensusVo> list = inventoryService.censusList(dto);
        return ResultVo.success("获取成功", list);
    }

    @ApiOperation(value = "导出结存")
    @RequestMapping(value = "/downLoadExportCensus", method = RequestMethod.GET)
    public void downLoadExportCensus(@RequestParam(value = "warehouseId", required = false) Integer warehouseId,
                                     @RequestParam(value = "commodityType", required = false) String  commodityType,
                                     @RequestParam(value = "previousTime", required = false) List<String> previousTime,
                                     @RequestParam(value = "time", required = false) List<String> time,
                                     HttpServletResponse response
    ) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("结存报表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<InventoryCensusExecl> orderList = inventoryService.exportCensus(warehouseId,commodityType,previousTime,time);
        //需要合并的列
        int[] mergeColumeIndex = {};
        // 从第二行后开始合并
        int mergeRowIndex = 15;
        EasyExcel.write(response.getOutputStream(), InventoryCensusExecl.class)
                .sheet("结存报表").doWrite(orderList);
              // .registerWriteHandler(new ExcelMergeUtil(mergeRowIndex, mergeColumeIndex))

    }

    @ApiOperation(value = "导入库存")
    @PostMapping("/uploadInventory")
    public ResultVo uploadInventory(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), InventoryExcelDto.class, new GoodsListener(inventoryService)).sheet().headRowNumber(1).doRead();
        return ResultVo.success("上传成功");
    }


    @ApiOperation(value = "结存")
    @GetMapping("/balanceList")
    public ResultVo balanceList(InventoryDto dto) {
        List<InventoryCensusVo> list = inventoryService.balanceList(dto);
        return ResultVo.success("获取成功", list);
    }


    @ApiOperation(value = "调整库存")
    @PostMapping("/findInventoryAdjust")
    public ResultVo findInventoryAdjust(@RequestBody  WarehouseAndCommodityVo dto) {
        return inventoryService.findInventoryAdjust(dto);
    }

    @ApiOperation(value = "退还货库存")
    @PostMapping("/refundOfGoods")
    public ResultVo refundOfGoods(@RequestBody  WarehouseAndCommodityVo dto) {
        return inventoryService.findInventoryAdjust(dto);
    }



    @ApiOperation(value = "修改库存")
    @GetMapping("/updateInventory/{id}/{count}")
    public ResultVo updateInventory(@PathVariable("id") Integer id,@PathVariable("count") String count) {
        inventoryService.update(new UpdateWrapper<Inventory>().set("goods_amount",new BigDecimal(count)).eq("id", id));
        return ResultVo.success("获取成功");
    }


}
